#include "stdafx.h"

#include "SocketClient.h"
#include "BaseStruct.h"
#include "DataParse.h"
#include "DataPackage.h"

SocketClient::SocketClient(QObject *parent)
	: QTcpSocket(parent)
{
	bAlive = false;
	connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
	connect(this, SIGNAL(readyRead()), this, SLOT(onDataReceived()));
	setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, TCP_MAX_BUFFER_SIZE);
	setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, TCP_MAX_BUFFER_SIZE);
}

SocketClient::~SocketClient()
{
	disconnect(this, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
	disconnect(this, SIGNAL(readyRead()), this, SLOT(onDataReceived()));
}

/***********************************************************************************************************************************/
bool SocketClient::connectToServer(const QHostAddress & addr, int port)
{
	CHK_EXP_RET(addr.isNull() || port == 0, false);
	connectToHost(addr, port);
	bAlive = waitForConnected(5000);
	return bAlive;
}

bool SocketClient::postHeartBeat()
{
	bAlive = false;
	bool bPost = false;
	DataPackage pack;
	QByteArray data;
	if (pack.request(data))
	{
		bPost = sendData(data);
		bPost = bPost && waitForReadyRead(5000);
	}
	return bPost & bAlive;
}

bool SocketClient::sendData(const QByteArray & send)
{
	qint64 size = write(send);
	waitForBytesWritten(1000);

	if (send.size() != size)
	{
		return false;
	}
	return true;
}

bool SocketClient::sendData(const QByteArray & send, qint64 & sendsize)
{
	sendsize = write(send);
	waitForBytesWritten(1000);

	if (send.size() != sendsize)
	{
		return false;
	}
	return true;
}

bool SocketClient::requestData(const QByteArray & send, QByteArray & received, QString & strError)
{
	strError.clear();
	qint64 size = write(send);
	waitForBytesWritten(1000);
	if (send.size() != size)
	{
		strError = tr("send data failed, data: %1, send: %2").arg(send.size(), size);
		return false;
	}

	if (!waitForReadyRead())
	{
		strError = tr("not received data back, please check connection!");
		return false;
	}

	received = arrReceivedData;
	return true;
}

/***********************************************************************************************************************************/
void SocketClient::onDisconnect()
{
	emit evtDisconnect(this);
}

void SocketClient::onDataReceived()
{
	DataParse parse;
	arrReceivedData.clear();

	while (bytesAvailable() > 0)
	{
		arrReceivedData.push_back(readAll());
		if (!parse.parse(arrReceivedData))
		{
			waitForReadyRead(10000);
		}

		//if (parse.getType() == JDT_HeartBeat)
		//{
		//	bAlive = true;
		//	QString strHeart;
		//	if (parse.getRequest(strHeart))
		//	{
		//		if (strHeart == "1")
		//		{
		//			DataPackage pack;
		//			QByteArray data;
		//			if (pack.response(data))
		//				sendData(data);
		//		}
		//	}
		//}
	}

	emit evtDataReceived(this, arrReceivedData);
}